package dal

import (
	"fmt"
	"time"

	. "7qule.com/sd3/CrossServer/src/bll/player/model"
	"7qule.com/sd3/CrossServer/src/common/onlinelog"
	baseDal "7qule.com/sd3/CrossServer/src/dal"
	"public.com/goutil/timeUtil"
)

// 用户信息访问结构
type globalPlayerDal struct{}

// 访问结构对象
var GlobalPlayerDal = new(globalPlayerDal)

// TableName 表名
func (this *globalPlayerDal) TableName() string {
	return "g_player"
}

// GetData 从数据库获取玩家数据
// dataContainer:玩家对象
// 返回值:
// interface{}:获取到的数据
// error:错误信息
func (this *globalPlayerDal) GetData() ([]*GlobalPlayer, error) {
	sql := "SELECT `Id`,`ServerGroupId`,`PartnerId`,`ServerId`,`ServerName`,`Name`,`GuildId`,`GuildName`,`Lv`,`Vip`,`Fap`,`MaxFap`,`HeadImageId`,`FashionModelId`,`FashionLevel`,`Flag`,`Logo`,`BaseInfo`,`ClientPlayerStr`,`ActiveTime` FROM `g_player` WHERE `ActiveTime` >= DATE_SUB(NOW(), INTERVAL 14 DAY);"

	db := baseDal.GetGameDB().DB()
	execResult, err := db.Query(sql)
	if err != nil {
		baseDal.WriteLog(sql, err)
		return nil, err
	}

	defer execResult.Close()

	// 读取数据
	var result = make([]*GlobalPlayer, 0, 16)
	for execResult.Next() {
		var tempNewItem = NewGlobalPlayer2()
		err = execResult.Scan(
			&tempNewItem.Id,
			&tempNewItem.ServerGroupId,
			&tempNewItem.PartnerId,
			&tempNewItem.ServerId,
			&tempNewItem.ServerName,
			&tempNewItem.Name,
			&tempNewItem.GuildId,
			&tempNewItem.GuildName,
			&tempNewItem.Lv,
			&tempNewItem.Vip,
			&tempNewItem.Fap,
			&tempNewItem.MaxFap,
			&tempNewItem.HeadImageId,
			&tempNewItem.FashionModelId,
			&tempNewItem.FashionLevel,
			&tempNewItem.Flag,
			&tempNewItem.Logo,
			&tempNewItem.BaseInfo,
			&tempNewItem.ClientPlayerStr,
			&tempNewItem.ActiveTime)

		if err != nil {
			baseDal.WriteLog(sql, err)
			return nil, err
		}

		result = append(result, tempNewItem)
	}

	return result, nil
}

// GetPlayerFromDb 从数据库获取玩家数据
// dataContainer:玩家对象
// 返回值:
// interface{}:获取到的数据
// error:错误信息
func (this *globalPlayerDal) GetPlayerFromDb(playerId string) (*GlobalPlayer, error) {
	sql := fmt.Sprintf("SELECT `Id`,`ServerGroupId`,`PartnerId`,`ServerId`,`ServerName`,`Name`,`GuildId`,`GuildName`,`Lv`,`Vip`,`Fap`,`MaxFap`,"+
		"`HeadImageId`,`FashionModelId`,`FashionLevel`,`Flag`,`Logo`,`BaseInfo`,`ClientPlayerStr`,`ActiveTime` FROM `g_player` WHERE Id='%v'",
		playerId,
	)

	db := baseDal.GetGameDB().DB()
	execResult, err := db.Query(sql)
	if err != nil {
		baseDal.WriteLog(sql, err)
		return nil, err
	}

	defer execResult.Close()

	// 读取数据
	for execResult.Next() {
		var tempNewItem = NewGlobalPlayer2()
		err = execResult.Scan(
			&tempNewItem.Id,
			&tempNewItem.ServerGroupId,
			&tempNewItem.PartnerId,
			&tempNewItem.ServerId,
			&tempNewItem.ServerName,
			&tempNewItem.Name,
			&tempNewItem.GuildId,
			&tempNewItem.GuildName,
			&tempNewItem.Lv,
			&tempNewItem.Vip,
			&tempNewItem.Fap,
			&tempNewItem.MaxFap,
			&tempNewItem.HeadImageId,
			&tempNewItem.FashionModelId,
			&tempNewItem.FashionLevel,
			&tempNewItem.Flag,
			&tempNewItem.Logo,
			&tempNewItem.BaseInfo,
			&tempNewItem.ClientPlayerStr,
			&tempNewItem.ActiveTime)

		if err != nil {
			baseDal.WriteLog(sql, err)
			return nil, err
		}

		return tempNewItem, nil
	}

	return nil, nil
}

// Replace 插入数据
// dataContainer:玩家对象
// redisData:redis数据
// data:操作的数据项
func (this *globalPlayerDal) Replace(data *GlobalPlayer) {
	sql := fmt.Sprintf("REPLACE INTO `g_player` (`Id`,`ServerGroupId`,`PartnerId`,`ServerId`,`ServerName`,`Name`,`GuildId`,`GuildName`,`Lv`,`Vip`,`Fap`,`MaxFap`,`HeadImageId`,`FashionModelId`,`FashionLevel`,`Flag`,`Logo`,`BaseInfo`,`ClientPlayerStr`,`ActiveTime`) VALUES ('%v', %v, %v, %v, '%v', '%v', '%v', '%v', %v, %v, %v, %v,%v,%v, %v, '%v', '%v', '%v', '%v','%v') ;",
		data.Id,
		data.ServerGroupId,
		data.PartnerId,
		data.ServerId,
		data.ServerName,
		data.Name,
		data.GuildId,
		data.GuildName,
		data.Lv,
		data.Vip,
		data.Fap,
		data.MaxFap,
		data.HeadImageId,
		data.FashionModelId,
		data.FashionLevel,
		data.Flag,
		data.Logo,
		data.BaseInfo,
		data.ClientPlayerStr,
		timeUtil.ToDateTimeString(data.ActiveTime))

	result := baseDal.GetGameDB().Exec(sql)
	if result.Error != nil {
		onlinelog.ErrorLog("执行sql:%v异常，错误信息:%v", sql, result.Error.Error())
	}
}

// Delete 删除数据
// redisData:最新redis数据
// data:操作的数据项
func (this *globalPlayerDal) Delete(ID string) {
	sql := fmt.Sprintf("DELETE FROM g_player where`Id`='%v';", ID)

	result := baseDal.GetGameDB().Exec(sql)
	if result.Error != nil {
		onlinelog.ErrorLog("执行sql:%v异常，错误信息:%v", sql, result.Error.Error())
	}
}

// GetGuildMembers 从数据库获取公会玩家数据
// 返回值:
// interface{}:获取到的数据
// error:错误信息
func (this *globalPlayerDal) GetGuildMembers(guildId string) ([]string, error) {
	sql := fmt.Sprintf("SELECT `Id` FROM `g_player` WHERE `GuildId`='%v' AND `ActiveTime` >= DATE_SUB(NOW(), INTERVAL 14 DAY);",
		guildId)

	db := baseDal.GetGameDB().DB()
	execResult, err := db.Query(sql)
	if err != nil {
		baseDal.WriteLog(sql, err)
		return nil, err
	}

	defer execResult.Close()

	// 读取数据
	var result = make([]string, 0, 16)
	for execResult.Next() {
		var tempNewItem = new(string)
		err = execResult.Scan(
			&tempNewItem)

		if err != nil {
			baseDal.WriteLog(sql, err)
			return nil, err
		}

		result = append(result, *tempNewItem)
	}

	return result, nil
}

// 清理数据库过期数据
// 返回值:
// error:错误信息
func (this *globalPlayerDal) ClearDb14DayOverdueData() {
	// 删除不活跃且等级小于70级的玩家
	sql := fmt.Sprintf("DELETE FROM g_player WHERE ActiveTime < '%s' AND Lv < 70;", timeUtil.ToDateTimeString(time.Now().AddDate(0, 0, -14)))

	result := baseDal.GetGameDB().Exec(sql)
	if result.Error != nil {
		onlinelog.ErrorLog("执行sql:%v异常，错误信息:%v", sql, result.Error.Error())
	}
}
